﻿using System;
using System.Collections.Generic;
using System.Web.Services.Protocols;
using System.Web.UI.WebControls;
using MedicWeb.MedicService;

namespace MedicWeb.forms.Editors.Directory {
    public partial class ComissonEdit : SessionPage {
        private Comission comission;

        protected void Page_Load(object sender, EventArgs e) {
            string id = Request.QueryString["id"];
            Form.Action = ResolveUrl("~/forms/Editors/Directory/ComissonEdit.aspx");
            var ms = new MainService();
            if (id != null) {
                Form.Action += "?id=" + id;
                comission = ms.LoadComission(GetSession(), int.Parse(id));
            } else {
                comission = new Comission();
                comission.Chairman = "";
                comission.Secretary = "";
                comission.Head = "";
                comission.Job = "";
                comission.Organisation = "";
                comission.Secretary = "";
                comission.Title = "";
            }
            if (!IsPostBack) {
                tfChairman.Text = comission.Chairman;
                tfHead.Text = comission.Head;
                tfJob.Text = comission.Job;
                tfOrganisation.Text = comission.Organisation;
                tfSecretary.Text = comission.Secretary;
                tfTitle.Text = comission.Title;
                ComissionMember[] comissionMemberList = ms.GetComissionMemberListByComission(GetSession(), comission);
                foreach (var member in comissionMemberList) {
                    var row = new TableRow();
                    var cellDelete = new TableCell();
                    cellDelete.Width = 18;
                    var cellMember = new TableCell();
                    cellDelete.Controls.Add(new HyperLink() {
                        ID = "" + member.Id,
                        NavigateUrl = "",
                        CssClass = "delete member"
                    });
                    cellMember.Text = member.Member;
                    hfMembers.Value += ";" + member.Id + "=" + member.Member;

                    row.Cells.Add(cellDelete);
                    row.Cells.Add(cellMember);
                    tblMembers.Rows.Add(row);
                }
            }

        }

        protected void btnSave_Click(object sender, EventArgs e) {
            try {
                var ms = new MainService();
                string valMem = hfMembers.Value;
                string[] splitMembers = valMem.Split(';');
                List<int> ids = new List<int>();
                comission.Chairman = tfChairman.Text;
                comission.Chairman = tfHead.Text;
                comission.Job = tfJob.Text;
                comission.Organisation = tfOrganisation.Text;
                comission.Secretary = tfSecretary.Text;
                comission.Title = tfTitle.Text;
                
                comission = ms.SaveComission(GetSession(), comission);
                for (int i = 1; i < splitMembers.Length; i++) {
                    string[] strings = splitMembers[i].Split('=');
                    int id = Convert.ToInt32(strings[0]);
                    string text = strings[1];
                    var comissionMember = new ComissionMember() { Comission = comission, Id = id, Member = text };
                    var member = ms.SaveComissionMember(GetSession(), comissionMember);
                    ids.Add(member.Id);
                }

                ms.SetComissionMembersToComission(GetSession(), comission, ids.ToArray());

                Session["success"] = "Изменения сохранены успешно";
            } catch (Exception ex) {

                if (ex.InnerException is SoapException
                    && ((SoapException)ex.InnerException).Detail.InnerText == "AuthorizationException") {
                    throw ex;
                } else {
                    Session["error"] = "Ошибка сохранения";
                }
            }
        }
        
    }
}